package com.paipaideli.ui.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gcssloop.widget.RCImageView;
import com.jakewharton.rxbinding2.view.RxView;
import com.paipaideli.R;
import com.paipaideli.common.utils.ImageUtil;
import com.paipaideli.common.utils.ToastUtil;
import com.paipaideli.ui.home.bean.HomeBean;
import com.paipaideli.ui.webView.WebAcyivity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页icon适配器
 */

public class DocResAdapter extends RecyclerView.Adapter<DocResAdapter.DocResViewHolder> {

    List<HomeBean.Data.SysDocRes> icons = new ArrayList<>();
    Context context;
    String indexId;
    String modelId;
    String splitColor;

    public DocResAdapter(Context context, List<HomeBean.Data.SysDocRes> icons, String indexId, String modelId, String splitColor) {
        this.icons = icons;
        this.context = context;
        this.indexId = indexId;
        this.modelId = modelId;
        this.splitColor = splitColor;
    }

    public DocResAdapter(Context context, List<HomeBean.Data.SysDocRes> icons) {
        this.icons = icons;
        this.context = context;
    }

    @Override
    public DocResViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_homepageicon, parent, false);
        convertView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return new DocResViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(DocResViewHolder holder, int position) {
        ImageUtil.displayImage(icons.get(position).imagePath, holder.homepageIcon);
        holder.homepageIconName.setText(icons.get(position).title);

        RxView.clicks(holder.llContent)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    if(!TextUtils.isEmpty(icons.get(position).externalLinks)){
                        WebAcyivity.open(context,icons.get(position).externalLinks,icons.get(position).title);
                    }
                });
    }

    @Override
    public int getItemCount() {
        return icons.size();
    }

    public class DocResViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.homepage_icon)
        RCImageView homepageIcon;
        @BindView(R.id.homepage_icon_name)
        TextView homepageIconName;
        @BindView(R.id.homepage_icon_new)
        ImageView homepageIconNew;
        @BindView(R.id.ll_content)
        LinearLayout llContent;

        public DocResViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
